﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using System.Data;

namespace Catering.UserTraining.SlrCompanyTrainning
{
    public partial class cpntraining_test_add : UserIsNull
    {
        public string m_UserID = "0";
        public int m_TestID = 0;
        public int m_LessonID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] != null) m_UserID = Session["UserID"].ToString();
            m_TestID = CommRequest.GetInt("TestID", 0);
            m_LessonID = CommRequest.GetInt("LessonID", 0);
            if (!IsPostBack)
            {
                this.lbl_TestName.Text = "新增题库";
                //增加或者修改判断。
                if (!string.IsNullOrEmpty(CommRequest.GetString("Flag")))
                {
                    this.hid_Flag.Value = CommRequest.GetString("Flag");
                }

                if (this.hid_Flag.Value == "EDIT")
                {
                    this.InitTestsInfo();
                }
            }
        }

        private void InitTestsInfo()
        {
            this.lbl_TestName.Text = new Catering_Rules.Tests().GetModel(m_TestID).TestName;
            Catering_Model.Tests _TestModel = new Catering_Model.Tests();
            Catering_Model.StaffLessonsTests _StaffLessonsTestModel = new Catering_Model.StaffLessonsTests();
            _TestModel = new Catering_Rules.Tests().GetModel(m_TestID);
            _StaffLessonsTestModel = new Catering_Rules.StaffLessonsTests().GetModel(m_LessonID, m_TestID);
            if (_TestModel == null) return;
            if (_StaffLessonsTestModel == null) return;
            ViewState["EditTestsModel"] = _TestModel;
            ViewState["EditStaffLessonsTestsModel"] = _StaffLessonsTestModel;
            this.txt_TestName.Text = _TestModel.TestName;
            this.rbl_Status.SelectedValue = _TestModel.Status;
            this.txt_ReMarks.Text = _StaffLessonsTestModel.Remarks;
        }

        /// <summary>
        /// 添加或者修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string sMsg = "";
            Catering_Model.Tests _TestsModel = new Catering_Model.Tests();
            _TestsModel.TestName = this.txt_TestName.Text.Trim();
            _TestsModel.TestType = "U";
            _TestsModel.Status = this.rbl_Status.SelectedValue;
            if (this.hid_Flag.Value == "EDIT")
            {
                Catering_Model.Tests _OldTestsModel = (Catering_Model.Tests)ViewState["EditTestsModel"];
                _TestsModel.TestID = m_TestID;
                _TestsModel.AddUserID = _OldTestsModel.AddUserID;
                _TestsModel.AddTime = _OldTestsModel.AddTime;
                new Catering_Rules.Tests().CheckTestModel(_TestsModel, out sMsg);
                if (string.IsNullOrEmpty(sMsg) && new Catering_Rules.Tests().Update(_TestsModel))
                {
                    UpdateStaffLessonsTests(m_LessonID,m_TestID);
                    RegisterStartupScript("保存成功!");
                }
                else
                {
                    RegisterStartupScript("保存失败！");
                }
            }
            else
            {
                _TestsModel.AddUserID = int.Parse(Session["UserID"].ToString());
                _TestsModel.AddTime = DateTime.Now;
                new Catering_Rules.Tests().CheckTestModel(_TestsModel, out sMsg);

                if (string.IsNullOrEmpty(sMsg))
                {
                    int intNewTestID = new Catering_Rules.Tests().Add(_TestsModel);
                    if (intNewTestID > 0)
                    {
                        AddStaffLessonsTests(intNewTestID);
                        RegisterStartupScript("保存成功!");
                    }
                    else
                    {
                        RegisterStartupScript("保存失败！");
                    }
                }
                else
                {
                    RegisterStartupScript("保存失败！");
                }
            }
        }

        public void AddStaffLessonsTests(int intNewTestID)
        {
            Catering_Model.StaffLessonsTests _LessonsModel = new Catering_Model.StaffLessonsTests();
            _LessonsModel.LessonID = m_LessonID;
            _LessonsModel.TestID = intNewTestID;
            _LessonsModel.AddUserID = int.Parse(Session["UserID"].ToString());
            _LessonsModel.AddTime = DateTime.Now;
            _LessonsModel.Remarks = this.txt_ReMarks.Text.Trim();
            new Catering_Rules.StaffLessonsTests().Add(_LessonsModel);
        }

        public void UpdateStaffLessonsTests(int intLessonID, int intTestID)
        {
            Catering_Model.StaffLessonsTests _LessonsModel = (Catering_Model.StaffLessonsTests)ViewState["EditStaffLessonsTestsModel"];
            _LessonsModel.Remarks = this.txt_ReMarks.Text;
            new Catering_Rules.StaffLessonsTests().Update(_LessonsModel);
        }

        public void RegisterStartupScript(string strMessage)
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "Affect", "alert('" + strMessage + "！')", true);
        }

        protected void btn_Return_Click(object sender, EventArgs e)
        {
            string url = "~/UserTraining/SlrCompanyTrainning/cpntraining_test_list.aspx?LessonID=" + m_LessonID;
            Response.Redirect(url);
        }
    }
}